package com.burgeon.backend.Controller;

import com.burgeon.backend.entity.MyStore;
import com.burgeon.backend.entity.Store;
import com.burgeon.backend.service.StoreService;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("store")
public class StoreController extends BaseController {

    @Autowired
    private StoreService storeService;

    @RequestMapping("all")
    public ResponseResult<ArrayList<Store>> GetStoreAll(String account){

        ArrayList<Store> data = storeService.GetStoreAll(account);
        return new ResponseResult<ArrayList<Store>>(SUCCESS,data);
    }

    @RequestMapping("info")
    public ResponseResult<Map> ConfigInfo(String account,String id){
        System.err.println(account+"'"+id);
        HashMap data = storeService.GetInfo(account,id);
        return new ResponseResult<>(SUCCESS,data);
    }

    @RequestMapping("upload")
    public ResponseResult<Void> Upload(@RequestParam(value = "file",required = false) MultipartFile file,String account,String store,String map,String sid){

        storeService.UploadData(file,account,store,sid,map);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("sync")
    public ResponseResult<Void> SynCity(String account){

        storeService.SyncStore(account);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("setcity")
    public ResponseResult<Void> SetCity(String city,HttpSession session){
        session.setAttribute("city",city);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("city")
    public ResponseResult<Map> CityStore(HttpSession session){
        String city = session.getAttribute("city").toString();
        String token = session.getAttribute("token").toString();
        Map data = storeService.GetStoreByCity(city,token);
        return new ResponseResult<Map>(SUCCESS,data);
    }

    @RequestMapping("mystore")
    public ResponseResult<List<MyStore>> MyStore(String city,HttpSession session){
        String token = session.getAttribute("token").toString();
        List<MyStore> data = storeService.GetStore(token,city);
        return new ResponseResult<List<MyStore>>(SUCCESS,data);
    }

    @RequestMapping("data")
    public ResponseResult<Map> GetStore(String start,String account){

        HashMap data = storeService.GetStoreData(account,start);
        return new ResponseResult<>(SUCCESS,data);
    }

    @RequestMapping("find")
    public ResponseResult<Map> FindWXUser(String start,String message,String account) {

        HashMap data = storeService.FindStoreData(account, start, message);
        return new ResponseResult<Map>(SUCCESS, data);
    }

}
